<!--
 * @Author: wangding
 * @Date: 2020-11-11 15:58:31
 * @LastEditTime: 2020-12-30 19:21:45
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\sale\add.vue
-->
<template>
    <div class="buyPage-main-box-add">
        <el-dialog
                :title="$t('edit')"
        :visible="newVisibleEdit"
        width="75%"
        @close="()=>{$store.commit('setNewVisibleEdit',false)}"
        >   
            <div style="padding-bottom:40px;">
                <div class="search-box">
                    <div class="search-box-sub">
                        <span class="lable-text">{{$t('devWifi.deviceSN')}}</span>
                        <el-input v-big v-model="subDeviceSN" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
                    </div>
                    <div class="search-box-sub">
                        <el-button type="primary" @click='query'>{{$t('query')}}</el-button>
                    </div>
                </div>
                <div class="button-box">
                    <el-popconfirm
                        :title="$t('index.delete_confirm')+'?'"
                        :confirm-button-text="$t('sure')"
                        :cancel-button-text="$t('cancel')"
                        @confirm='upgradegroupwifiDeleteBatch()'
                    >
                        <el-button type="danger" slot="reference" icon='el-icon-delete' >{{$t('delete')}}</el-button>
                    </el-popconfirm>
                </div>
                <baseTable v-loading="loading"  row-key='sid' :data="dataRows" border  stripe   :column='column' :select='true' @selection-change="handleSelectionChange($event)">
                    <template #deviceType="{data}">
                        <span>{{comm.returnCode(dictionary.SCOPE_TO,data.deviceType)}}</span>
                    </template>
                    <template #inverterType="{data}">
                        <span>{{comm.returnCode(dictionary.INVERTER_TYPES,data.inverterType)}}</span>
                    </template>
                    <template #armVersion="{data}">
                        <span>{{formatterValuearm(data.armVersion,data)}}</span>
                    </template>
                    <template #brandVersionFormatter="{data}">
                        <span >{{brandVersionFormatter(data)}}</span>
                    </template>
                    <template #option="{data}">
                        <div class="option-box">
                            <el-popconfirm
                                :title="$t('index.delete_item_confirm')+'?'"
                                @confirm='deleteSpa(data.deviceSN)'
                            >
                                <el-button type="danger" slot="reference" icon='el-icon-delete' :title="$t('delete')" ></el-button>
                            </el-popconfirm>
                        </div>
                    </template>
                </baseTable>
                <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="current"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
                </el-pagination>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import baseTable from '../../../../components/common/baseTable'
export default {
    components: {
        baseTable
    },
    props: {
        addValueList:{
            type:Array,
            default:()=>{return []}
        },
        selectDataList:{
            type:Array,
            default:()=>{return []}
        },
        inverterType:{
            type:String,
            default:''
        },
        scope:{
            type:String,
            default:''
        },
        groupId:{
            type:String,
            default:''
        }
    },
    data() {
        return {
            dictionary:{},
            subDeviceSN:'',
            loading:false,
            dataRows:[],
            selectList:[],
            size:10,
            current:1,
            total:1,
            column:[{
                title: this.$t('devWifi.deviceSN'),
                value: 'deviceSN',
                slot: 'deviceSN',
                min_width:'100'
            },{
                title: this.$t('devWifi.wifiSN'),
                value: 'wifiSN',
                slot: 'wifiSN',
                min_width:'100'
            },
            {
                title: this.$t('device-communication-type'),
                value: 'deviceType',
                slot: 'deviceType',
            },
            {
                title: this.$t('devWifi.deviceType'),
                value: 'inverterType',
                slot: 'inverterType',
                min_width:'100'
            },{
                title: this.$t('update.batteryModel'),
                value: 'wifiVersion',
                slot: 'brandVersionFormatter',
                
            },
            {
                title: this.$t('devWifi.wifiVersion'),
                value: 'wifiVersion',
                slot: 'wifiVersion',
                min_width:'100'
            },
            {
                title: this.$t('update.ARM'),
                value: 'armVersion',
                slot: 'armVersion',
            },
            {
                title: this.$t('update.DSP'),
                value: 'inverterVersion',
                slot: 'inverterVersion',
            },
            {
                title: this.$t('update.slaverDSP'),
                value: 'inverterSlaverVer',
                slot: 'inverterSlaverVer',
            },{
                title: this.$t('update.batteryM'),
                value: 'batteryMasterVer',
            },
            {
                title: this.$t('update.batteryS'),
                value: 'batterySlaverVer',
                slot: 'batterySlaverVer',
            },{
                title: this.$t('opttask.option'),
                value: 'sid',
                slot: 'option',
                // winth:'60'
            },],
            vppGroup:'',
            form:{
                addVppGroup:""
            },
            cloudType:[{
                id:'0',
                text:'Solax Cloud'
            },{
                id:'1',
                text:'QCELLS'
            }]
        };
    },
    computed: {
        newVisibleEdit(){
            return this.$store.state.sysStore.newVisibleEdit
        }
    },
    watch: {

    },
    created() {

    },
    mounted() {
        this.getDictionaryBatch()
        this.upgradegroupwifiGetPage()
    },
    methods: {
        brandVersionFormatter(row){
            let bv = ''
            if(row.batteryBrand != null && row.batterySlaverType != null){
                bv = parseInt(row.batteryBrand) * 10 +  parseInt(row.batterySlaverType);
                return this.comm.returnCode(this.dictionary.BATTERY_BRAND_VERSION, bv);
            }else{
                return '-';
            }
        },
        async upgradegroupwifiDeleteBatch(){
            let res = await this.API.upgradegroupwifiDeleteBatch({
                upgradeGroupId:this.groupId,
                inverterSns:this.selectList.map(item =>{
                    return item.deviceSN
                })
            })
            if(this.comm.showMsg(res.success)){
                this.upgradegroupwifiGetPage()
            }
        },
        async deleteSpa(sn){
            let res = await this.API.upgradegroupwifiDeleteBatch({
                upgradeGroupId:this.groupId,
                inverterSns:sn
            })
            if(this.comm.showMsg(res.success)){
                this.upgradegroupwifiGetPage()
            }
        },
        async getDictionaryBatch(){
            let res = await this.API.getDictionaryBatch({
                dictCodes:'SCOPE_TO,INVERTER_TYPES,DEVICE_CLASSIFY,PUBLISH_STATUS,BATTERY_VERSION,BATTERY_BRAND,BATTERY_VERSION_81,BATTERY_VERSION_82,BATTERY_VERSION_83,BATTERY_VERSION_84,BATTERY_BRAND_VERSION,ON_LINE_STATE',
                lang:localStorage.getItem('lang')||'zh_CN'
            })
            this.dictionary = res || {}
        },
        async upgradegroupwifiGetPage(){
            this.loading = true
            let res = await this.API.upgradegroupwifiGetPage({
                upgradeGroupId:this.groupId,
                deviceSN:this.subDeviceSN,
                size:this.size,
                current:this.current,
            })
            this.dataRows = res.rows || []
            this.loading = false
        },
        query(){
            this.upgradegroupwifiGetPage()
        },
        formatterValuearm(value,row){
            if(value){
                if(row.loaderVersion&&row.loaderVersion!='null'&&row.loaderVersion){
                    return value==null||value=='null'? "-":value+'-'+row.loaderVersion
                }else{
                    return value==null||value=='null'?'-':value
                }
            }
        },
        handleCurrentChange(val) {
          this.current = val
          this.upgradegroupwifiGetPage()
        },
        handleSizeChange(val) {
          this.size = val
          this.current = 1
          this.upgradegroupwifiGetPage()
        },
        query(){
            this.current = 1
            this.upgradegroupwifiGetPage()
        },
        handleSelectionChange(val){
            this.selectList = val
        }
    },
};
</script>

<style  lang="less" >
.buyPage-main-box-add{
    .el-form-item__content{
        display: flex;
    }
    .icon-fontsize{
        font-size: 25px;
        margin-left: 10px;
        display: block;
        width: 25px;
        height: 33px;
    }
}
</style>
